package ldh.serialize.type;

import java.io.IOException;

import ldh.serialize.BinarySerialized;
import ldh.serialize.io.ByteBuf;
import ldh.serialize.util.SerializedUtil;

public class ByteSerialized implements BinarySerialized<Byte> {

	@Override
	public void write(ByteBuf out, Byte bb) throws IOException {
		if (bb == null) {
			SerializedUtil.writeByte(out, (byte)0);
			return;
		}
		SerializedUtil.writeByte(out, (byte)1);
		SerializedUtil.writeByte(out, bb);
	}

	@Override
	public Byte read(ByteBuf in) throws IOException {
		byte isnull = SerializedUtil.readByte(in);
		if (isnull == 0) return null;
		return SerializedUtil.readByte(in);
	}

	@Override
	public Class<Byte> getClassType() {
		return Byte.class;
	}

}
